⚡ Optimize tensor creation in agent.py#42
⚡ Optimize tensor creation in agent.py#42google-labs-jules[bot] wants to merge 1 commit intomainfrom
Conversation
I replaced `torch.tensor(...).to(device)` with `torch.as_tensor(..., device=device)` for all batch tensors (action, reward, termination signal, weights, gamma) and state components in `optimize_model`. This change avoids unnecessary intermediate CPU tensor creation and data copying, resulting in a ~1.25x speedup in tensor creation overhead (measured on CPU, likely higher on GPU due to reduced host-device traffic). I verified these changes with `benchmark_tensor_creation.py` and `pytest tests/test_model.py`.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
This PR optimizes the tensor creation process in the
DDQNAgent.optimize_modelmethod.💡 What:
torch.tensor(data).to(device)withtorch.as_tensor(data, device=device).action_batch,reward_batch,done_batch,weights_batch,gamma_batch.tests/test_model.py(gen2.model->model) to enable proper verification.🎯 Why:
torch.as_tensorwith thedeviceargument allows creating the tensor directly on the target device (or copying directly to it), skipping the intermediate CPU tensor object overhead.📊 Measured Improvement:
benchmark_tensor_creation.py) simulating batch creation (batch_size=64).PR created automatically by Jules for task 11727680076001158903 started by @dzaczek